<template>
  <div class="course-upload">
    <el-form
      ref="courseForm"
      :model="courseData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="课程标题" prop="title">
        <el-input v-model="courseData.title" />
      </el-form-item>

      <el-form-item label="课程描述" prop="description">
        <el-input
          v-model="courseData.description"
          type="textarea"
          :rows="4"
        />
      </el-form-item>

      <el-form-item label="课程封面" prop="coverImage">
        <el-upload
          class="cover-uploader"
          :http-request="uploadCover"
          :show-file-list="false"
        >
          <img
            v-if="courseData.coverImage"
            :src="courseData.coverImage"
            class="cover-image"
          >
          <el-icon v-else class="upload-icon"><Plus /></el-icon>
        </el-upload>
      </el-form-item>

      <el-form-item label="课程价格" prop="price">
        <el-input-number
          v-model="courseData.price"
          :min="0"
          :precision="2"
        />
      </el-form-item>

      <el-form-item label="课程章节">
        <div
          v-for="(chapter, chapterIndex) in courseData.chapters"
          :key="chapterIndex"
          class="chapter-item"
        >
          <div class="chapter-header">
            <el-input v-model="chapter.title" placeholder="章节标题" />
            <el-button
              type="danger"
              icon="Delete"
              @click="removeChapter(chapterIndex)"
            />
          </div>

          <div
            v-for="(lesson, lessonIndex) in chapter.lessons"
            :key="lessonIndex"
            class="lesson-item"
          >
            <el-input v-model="lesson.title" placeholder="课时标题" />
            <el-upload
              class="video-upload"
              :http-request="(file) => uploadVideo(file, chapterIndex, lessonIndex)"
            >
              <el-button type="primary">上传视频</el-button>
            </el-upload>
            <el-button
              type="danger"
              icon="Delete"
              @click="removeLesson(chapterIndex, lessonIndex)"
            />
          </div>

          <el-button
            type="primary"
            plain
            @click="addLesson(chapter)"
          >
            添加课时
          </el-button>
        </div>

        <el-button
          type="primary"
          @click="addChapter"
        >
          添加章节
        </el-button>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          :loading="saving"
          @click="saveCourse"
        >
          保存课程
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import { uploadVideo, uploadCoverImage, createCourse } from '@/api/courses'
import { ElMessage } from 'element-plus'

const courseData = ref({
  title: '',
  description: '',
  coverImage: '',
  price: 0,
  chapters: []
})

const saving = ref(false)

const rules = {
  title: [{ required: true, message: '请输入课程标题', trigger: 'blur' }],
  description: [{ required: true, message: '请输入课程描述', trigger: 'blur' }],
  coverImage: [{ required: true, message: '请上传课程封面', trigger: 'change' }],
  price: [{ required: true, message: '请设置课程价格', trigger: 'blur' }]
}

const uploadCover = async ({ file }) => {
  try {
    const { url } = await uploadCoverImage(file)
    courseData.value.coverImage = url
  } catch (error) {
    ElMessage.error('上传封面失败')
  }
}

const uploadVideo = async ({ file }, chapterIndex, lessonIndex) => {
  try {
    const { url } = await uploadVideo(file)
    courseData.value.chapters[chapterIndex].lessons[lessonIndex].videoUrl = url
  } catch (error) {
    ElMessage.error('上传视频失败')
  }
}

const addChapter = () => {
  courseData.value.chapters.push({
    title: '',
    lessons: []
  })
}

const addLesson = (chapter) => {
  chapter.lessons.push({
    title: '',
    videoUrl: ''
  })
}

const removeChapter = (index) => {
  courseData.value.chapters.splice(index, 1)
}

const removeLesson = (chapterIndex, lessonIndex) => {
  courseData.value.chapters[chapterIndex].lessons.splice(lessonIndex, 1)
}

const saveCourse = async () => {
  saving.value = true
  try {
    await createCourse(courseData.value)
    ElMessage.success('课程创建成功')
  } catch (error) {
    ElMessage.error('保存课程失败')
  } finally {
    saving.value = false
  }
}
</script>

<style scoped>
.course-upload {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.cover-uploader {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.cover-image {
  width: 200px;
  height: 200px;
  object-fit: cover;
}

.upload-icon {
  font-size: 28px;
  color: #8c939d;
  width: 200px;
  height: 200px;
  line-height: 200px;
  text-align: center;
}

.chapter-item {
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
}

.chapter-header {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.lesson-item {
  display: flex;
  gap: 10px;
  margin: 10px 0;
  align-items: center;
}
</style>